"""
用于配置wsl2容器中的mysql数据库
"""

import pymysql

# 配置连接信息
connection = pymysql.connect(
    host='172.19.251.204',  # WSL 2 的 IP 地址
    user='root',
    password='123456',  # 替换为你的 root 密码
    port=3306,
)

try:
    with connection.cursor() as cursor:
        # 创建数据库 meiduo_mall
        create_db_query = "CREATE DATABASE IF NOT EXISTS meiduo_mall CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
        cursor.execute(create_db_query)

        # 创建用户 meiduo_user
        create_user_query = "CREATE USER IF NOT EXISTS 'meiduo_user'@'%' IDENTIFIED BY '123456';"
        cursor.execute(create_user_query)

        # 授予 meiduo_user 用户对 meiduo_mall 数据库的所有权限
        grant_privileges_query = "GRANT ALL PRIVILEGES ON meiduo_mall.* TO 'meiduo_user'@'%';"
        cursor.execute(grant_privileges_query)

        # 刷新权限
        cursor.execute("FLUSH PRIVILEGES;")

    print("Database and user created successfully.")
finally:
    connection.close()